package com.fyzk.activity.lottery.recommend_order;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;

import com.tyjc.lottery.R;

/**
 * 晒单方式
 */
public class RecommendTypePopupwindow extends PopupWindow implements View.OnTouchListener{

	private Context context;
	private String[] items = {"不发起推荐","公开推荐","保密推荐"};
	private View view;
	private GridView gv;
	private TextView cancel_tv;
	private TextView sure_tv;
	private int checkItem = 0;
	private int checkItemOld = 0;
	private RecommendTypeItemListener listener;
	private RecommendTypeAdapter adapter;

	public RecommendTypePopupwindow(Context context, String[] items){
		super(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		this.context = context;
		View view_pop = LayoutInflater.from(context).inflate(R.layout.popup_recommendtype, null);
		this.view = view_pop;
		this.items = items;
		this.view.setOnTouchListener(this);
		initView();
		this.setContentView(view);
		this.setFocusable(true);
		this.setOutsideTouchable(false);
		// 刷新状态
		this.update();
		ColorDrawable dw = new ColorDrawable(0000000000);
		this.setBackgroundDrawable(dw);
		this.setAnimationStyle(R.style.keyboard_anim);
	}


	@Override
	public boolean onTouch(View v, MotionEvent event) {
//		this.dismiss();
		popupDismiss(true);
		return true;
	}

	private void initView(){
		gv = (GridView) view.findViewById(R.id.recommendtype_gv);
		adapter = new RecommendTypeAdapter(items);
		gv.setEnabled(false);
		gv.setAdapter(adapter);

		cancel_tv = (TextView) view.findViewById(R.id.recommendtype_cancel);
		sure_tv = (TextView) view.findViewById(R.id.recommendtype_sure);

		cancel_tv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				popupDismiss(true);
			}
		});

		sure_tv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				popupDismiss(false);
			}
		});
	}


	/**
	 * 显示popupWindow
	 *
	 * @param parent
	 */
	public void showPopupWindow(View parent) {
		checkItemOld = checkItem;
		if (!this.isShowing()) {
			super.showAtLocation(parent, Gravity.BOTTOM,0,0);
		} else {
			this.dismiss();
		}
	}

	private void popupDismiss(boolean isCancel){
		if (!isCancel) {
			if (listener != null) {
				checkItemOld = checkItem;
				listener.onItem(checkItem);
			}
		} else {
			checkItem = checkItemOld;
		}
		this.dismiss();
	}

	@Override
	public void dismiss() {
		super.dismiss();
	}

	public void setCheckItem(int checkItem){
		this.checkItem = checkItem;
	}

	public int getCheckItem(){
		return this.checkItem;
	}

	//===== 接口	==========
	public interface RecommendTypeItemListener {
		void onItem(int position);
	}

	public void setListener(RecommendTypeItemListener listener) {
		this.listener = listener;
	}

	public void setItems(String[] items){
		this.items = items;
		adapter.setItems(items);
	}

	//==========================================================================
	class RecommendTypeAdapter extends BaseAdapter {
		private String[] items;

		public RecommendTypeAdapter(String[] items){
			this.items = items;
		}

		@Override
		public int getCount() {

			return items.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			String item = items[position];
			convertView = LayoutInflater.from(context).inflate(R.layout.popup_item_recommendtype, null);
			RadioButton checkBox = (RadioButton) convertView.findViewById(R.id.recommendtype_item_cb);

			checkBox.setText(item);
			checkBox.setChecked(checkItem == position);
			checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (isChecked) {
						if (checkItem != position) {
							checkItem = position;
						}

						notifyDataSetChanged();
					}
				}
			});

			return convertView;
		}
		public void setItems(String[] items){
			this.items = items;
			this.notifyDataSetChanged();
		}

	}


	public void setNumColumns(int numColumns){
		this.gv.setNumColumns(numColumns);
	}


}
